package com.example.springbootquartz.controller;

import static org.quartz.CronScheduleBuilder.cronSchedule;

import com.example.springbootquartz.schedule.TestJob2;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("test")
public class TestController {
  @Autowired
  private Scheduler scheduler;

  @GetMapping("/addJob")
  public void addJob() throws InterruptedException, SchedulerException {
    //创建触发器 定义执行频率
    String id = "3333344444";
    Trigger trigger = TriggerBuilder.newTrigger()
        .withIdentity(id, "group1")
        .withSchedule(cronSchedule("0/5 * * * * ?")).build();
    //创建任务，指定任务处理逻辑、如果任务已经存在则会报错
    JobDetail jobDetail = JobBuilder.newJob().ofType(TestJob2.class).withIdentity(id).build();
    scheduler.scheduleJob(jobDetail, trigger);

  }
  @GetMapping("/delJob")
  public void delJob() throws SchedulerException {
    List<JobKey> jobKeys = new ArrayList<>();
    JobKey jobKey1 = JobKey.jobKey("3333344444");
    jobKeys.add(jobKey1);
    scheduler.deleteJobs(jobKeys);
  }

}
